<template>
  <div class="dashboard-container">
    <div class="search-container">
      <!-- 搜索列表 -->
      <el-form ref="formInline" :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item prop="projectName">
          <el-input v-model="formInline.projectName" placeholder="请输入项目名" />
        </el-form-item>
        <el-form-item prop="startTime">
          <el-date-picker v-model="formInline.startTime" type="date" placeholder="开始日期" style="width: 100%;" />
        </el-form-item>
        <el-form-item prop="endTime">
          <el-time-picker v-model="formInline.endTime" type="date" placeholder="结束日期" style="width: 100%;" />
        </el-form-item>
        <el-form-item prop="projectStatus">
          <el-select v-model="formInline.projectStatus" placeholder="请选择项目状态">
            <el-option label="挂起" value="挂起" />
            <el-option label="正常" value="正常" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 添加按钮 -->
      <el-button type="primary" @click="handleAdd">新建</el-button>
    </div>
    <!-- 数据列表 -->
    <el-table
      v-loading="loading"
      :data="tableData"
      border
      style="width: 100%"
    >
      <el-table-column
        type="index"
        :index="handleI"
        label="编号"
        width="150"
      />
      <el-table-column
        prop="projectName"
        label="项目名称"
      />
      <el-table-column
        prop="projectHead"
        label="负责人"
      />
      <el-table-column
        prop="startTime"
        label="开始日期"
      />
      <el-table-column
        prop="endTime"
        label="结束日期"
      />
      <el-table-column
        prop="projectStatus"
        label="项目状态"
      />
      <el-table-column
        fixed="right"
        label="操作"
        width="150"
      >
        <template #default="{row}">
          <el-button size="mini" @click="handleEdit(row.id)">修改</el-button>
          <el-button type="danger" size="mini" @click="handleDel(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      :current-page="params.pageNo"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="params.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 表单对话框 -->
    <el-dialog v-loading="dialogLoading" :title="title" :visible.sync="dialogFormVisible" width="30%" @close="handleClose">
      <el-form ref="form" :model="form" :rules="rules" :label-width="formLabelWidth">
        <el-form-item prop="projectName" label="项目名:">
          <el-input v-model="form.projectName" placeholder="请输入项目名" />
        </el-form-item>
        <el-form-item prop="projectHead" label="负责人:">
          <el-input v-model="form.projectHead" placeholder="请输入负责人名称" />
        </el-form-item>
        <el-form-item prop="startTime" label="开始日期:">
          <el-date-picker v-model="form.startTime" type="date" placeholder="请输入开始日期" style="width: 100%;" />
        </el-form-item>
        <el-form-item prop="endTime" label="结束日期:">
          <el-date-picker v-model="form.endTime" type="date" placeholder="请输入结束日期" style="width: 100%;" />
        </el-form-item>
        <el-form-item prop="projectStatus" label="项目状态:">
          <el-select v-model="form.projectStatus" placeholder="请选择项目状态">
            <el-option label="挂起" value="挂起" />
            <el-option label="正常" value="正常" />
          </el-select>
        </el-form-item>
        <el-form-item label="项目介绍:">
          <el-upload
            action="#"
            :file-list="fielList"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :http-request="handleChange"
            :on-remove="handleRemove"
          >
            <i class="el-icon-plus" />
          </el-upload>
          <el-dialog :visible.sync="dialogVisible" :modal="false">
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getDatas, getData, updateData, delData, addData } from '@/api/maintenance'
export default {
  name: 'Dashboard',
  data() {
    return {
      loading: false,
      dialogLoading: false,
      tableData: [],
      dialogFormVisible: false,
      form: {
        projectName: '',
        projectHead: '',
        startTime: '',
        endTime: '',
        projectStatus: '',
        projectIntroduction: ''
      },
      formLabelWidth: '120px',
      formInline: {
        projectName: '',
        startTime: '',
        endTime: '',
        projectStatus: ''
      },
      title: '',
      dialogImageUrl: '',
      dialogVisible: false,
      fielList: [],
      img: [],
      params: {
        pageSize: 10,
        pageNo: 1
      },
      total: 0,
      rules: {
        projectName: [{ required: true, message: '请输入项目名', trigger: 'blur' }],
        projectHead: [{ required: true, message: '请输入负责人名称', trigger: 'blur' }],
        startTime: [{ required: true, message: '请输入开始日期', trigger: 'blur' }],
        endTime: [{ required: true, message: '请输入结束日期', trigger: 'blur' }],
        projectStatus: [{ required: true, message: '请选择项目状态', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getData(this.params)
  },
  methods: {
    async getData(params) {
      this.loading = true
      const res = await getDatas({
        ...params,
        pageNo: this.params.pageNo - 1
      })
      this.tableData = res.content
      this.loading = false
      this.total = res.totalElements
    },
    handleDel(id) {
      this.$confirm('此操作将永久删除该条数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delData(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        if (this.tableData.length === 1) {
          this.pageNo--
        }
        this.getData(this.params)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    onSubmit() {
      this.getData(this.formInline)
    },
    handleAdd() {
      this.title = '添加能效检测点'
      this.dialogFormVisible = true
    },
    async handleEdit(id) {
      this.title = '修改能效检测点'
      this.dialogFormVisible = true
      this.dialogLoading = true
      this.form = await getData(id)
      this.dialogLoading = false
    },
    handleReset() {
      this.$refs['formInline'].resetFields()
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handleChange({ file }) {
      this.img.push(file)
      const url = URL.createObjectURL(file)
      this.fielList.push({ url })
    },
    handleSizeChange(val) {
      this.params.pageSize = val
      this.getData(this.params)
    },
    handleCurrentChange(val) {
      this.params.pageNo = val
      this.getData(this.params)
    },
    handleRemove(file, fielList) {
      const index = this.fielList.findIndex(item => item === file)
      this.img = this.img.splice(index, 1)
      this.fielList = fielList
    },
    handleI(i) {
      return this.params.pageSize * (this.params.pageNo - 1) + i + 1
    },
    handleSubmit() {
      this.$refs.form.validate(async flag => {
        if (flag) {
          const formData = new FormData()
          formData.append('img', this.img)
          formData.append('maintenance', new Blob([JSON.stringify({ ...this.form })], {
            type: 'application/json'
          }))
          if (this.form.id) {
            await updateData(this.form.id, formData)
            this.$message.success('更新成功')
          } else {
            await addData(formData)
            this.$message.success('添加成功')
          }
        }
        this.dialogFormVisible = false
        this.getData(this.params)
      })
    },
    handleClose() {
      this.$refs.form.resetFields()
      this.form = {
        projectName: '',
        projectHead: '',
        startTime: '',
        endTime: '',
        projectStatus: '',
        projectIntroduction: ''
      }
      this.img = []
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard-container {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
  .search-container {
    box-sizing: border-box;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px 0;
    ::v-deep .el-form-item {
      margin-bottom: 0;
    }
  }
}
</style>

